<?php
namespace Admin\Controller;

class WithdrawController extends AdminController{

	public function index(){
        $list   = $this->lists('member_withdraw');
        foreach ($list as $key => &$value) {
        	$value['uname'] = M('ucenter_member')->where(array('id'=>$value['u_id']))->getField('username');
        	$value['erea'] = $value['price']*0.01;
        }
        // dump($list);die();
        $this->assign('_list', $list);
        $this->meta_title = '提现列表';
		$this->display();
	}
	public function runwithdraw(){
		$id = I('id');
		$WIDtrade_no = I('WIDtrade_no');
		$info = M('member_withdraw')->find($id);
		if (!empty($info['WIDtrade_no']) && $info['type']) {
			$this->error("已处理");
		}

		// 开启事务
		M('member_withdraw')->startTrans();
		$data = array(
			'id'	=> $id,
			'WIDtrade_no'=>$WIDtrade_no,
			'type'	=> 1
		);
		$r1 = M('member_withdraw')->save($data);

		$r2 = M('member_bill')->save(array('id'=>$id,'state'=>1));
		if ($r1 && $r2) {
			M('member_withdraw')->commit();
    		$this->success('处理成功');
		}else{
			M("member_withdraw")->rollback();
			$this->error("处理失败");
		}
	}

    /**
     * 充值列表
     */
	public  function   rechargelist(){
	    $where['status'] = 1;
        $list   = $this->lists('recharge_order',$where);
        foreach ($list as $key => &$value) {
            $value['uname'] = M('ucenter_member')->where(array('id'=>$value['uid']))->getField('username');
        }
        $this->assign('_list', $list);
        $this->meta_title = '充值列表';

        $this->display();
    }

    /**
     * 给客户充值
     */
    public  function  rechargeadd(){
        $ucenter_member =M('ucenter_member');
        if($_POST) {
            $username = I('username');
            $amount = I('amount');
            $where['username'] = $username;
            $uid = $ucenter_member->where($where)->getField('id');
            $wh['id'] = $uid;
            if ($uid) {
                $res = M('Member')->where($wh)->setInc('amount', $amount);
            }else{
                $this->error('用户不存在', U('rechargeadd'));
            }
            if ($res) {

                $data = array(
                    'uid'              =>  $uid,
                    'order_no'     =>$this->build_order_no(),
                    'amount'       => I('amount'),
                    'status'          => 1,
                    'create_time' => NOW_TIME,
                    'update_time'=>NOW_TIME
                );
                $recharge = M('recharge_order');
                $InsertId   = $recharge->add($data);

                $this->success('充值成功', U('rechargelist'));
            }else{
                $this->error('充值失败', U('rechargeadd'));
            }

        }else {
            $this->display();
        }
    }

    /*
  * 生成订单号
  *
  */
    function build_order_no()
    {
        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);

        return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }
}